package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.system.SysNoticeGroup;
import com.ruoyi.system.service.system.ISysNoticeGroupService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公告公示类型Controller
 *
 * @author ruoyi
 * @date 2025-06-04
 */
@Validated
@Api(value = "公告公示类型控制器", tags = {"公告公示类型管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/group")
public class SysNoticeGroupController extends BaseController {

    private final ISysNoticeGroupService sysNoticeGroupService;
	@Value("${ruoyi.imgUrl}")
	private String imgUrl;
    /**
     * 查询公告公示类型列表
     */
    @ApiOperation("查询公告公示类型列表")
    @PreAuthorize("@ss.hasPermi('system:group:list')")
    @GetMapping("/list")
    public AjaxResult list(@Validated SysNoticeGroup sysNoticeGroup) {
        QueryWrapper<SysNoticeGroup> qw = new QueryWrapper<>();

        List<SysNoticeGroup> list = sysNoticeGroupService.list(qw);
		for (SysNoticeGroup list1:list){
			if (list1.getLogo()!=null){
				list1.setLogoPath(imgUrl+list1.getLogo());
			}
		}

        return AjaxResult.success(list);
    }

	/**
	 * 查询公告公示类型列表
	 */
	@ApiOperation("查询公告公示类型列表")
	@PreAuthorize("@ss.hasPermi('system:group:list')")
	@GetMapping("/parentList")
	public AjaxResult parentList(@Validated SysNoticeGroup sysNoticeGroup) {
		QueryWrapper<SysNoticeGroup> qw = new QueryWrapper<>();
		qw.eq("level",0);
		List<SysNoticeGroup> list = sysNoticeGroupService.list(qw);
		return AjaxResult.success(list);
	}



    /**
     * 导出公告公示类型列表
     */
    @ApiOperation("导出公告公示类型列表")
    @PreAuthorize("@ss.hasPermi('system:group:export')")
    @Log(title = "公告公示类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysNoticeGroup> export(@Validated SysNoticeGroup sysNoticeGroup) {
        List<SysNoticeGroup> list = sysNoticeGroupService.list();
        ExcelUtil<SysNoticeGroup> util = new ExcelUtil<SysNoticeGroup>(SysNoticeGroup.class);
        return util.exportExcel(list, "公告公示类型");
    }

    /**
     * 获取公告公示类型详细信息
     */
    @ApiOperation("获取公告公示类型详细信息")
    @PreAuthorize("@ss.hasPermi('system:group:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysNoticeGroup> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        SysNoticeGroup sysNoticeGroup = sysNoticeGroupService.getById(id);
		if (sysNoticeGroup.getLogo()!=null){
			sysNoticeGroup.setLogoPath(imgUrl+sysNoticeGroup.getLogo());
		}
        return AjaxResult.success(sysNoticeGroup);
    }

    /**
     * 新增公告公示类型
     */
    @ApiOperation("新增公告公示类型")
    @PreAuthorize("@ss.hasPermi('system:group:add')")
    @Log(title = "公告公示类型", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SysNoticeGroup sysNoticeGroup) {
        return toAjax(sysNoticeGroupService.save(sysNoticeGroup) ? 1 : 0);
    }

    /**
     * 修改公告公示类型
     */
    @ApiOperation("修改公告公示类型")
    @PreAuthorize("@ss.hasPermi('system:group:edit')")
    @Log(title = "公告公示类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysNoticeGroup sysNoticeGroup) {
        return toAjax(sysNoticeGroupService.updateById(sysNoticeGroup) ? 1 : 0);
    }

    /**
     * 删除公告公示类型
     */
    @ApiOperation("删除公告公示类型")
    @PreAuthorize("@ss.hasPermi('system:group:remove')")
    @Log(title = "公告公示类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(sysNoticeGroupService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
